package com.pxl.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.message.ChatMessageForm;
import com.pxl.springbootinit.model.entity.ChatMessage;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.vo.UserVO;
import com.pxl.springbootinit.service.ChatMessageService;
import com.pxl.springbootinit.mapper.ChatMessageMapper;
import com.pxl.springbootinit.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author MADAO
* @description 针对表【chat_message(即时通讯表)】的数据库操作Service实现
* @createDate 2024-12-20 11:59:03
*/
@Service
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ChatMessage>
    implements ChatMessageService{

    @Resource
    private UserService userService;

    @Override
    public Page<ChatMessage> pageMessage(ChatMessageForm chatMessageForm) {
        int current = chatMessageForm.getCurrent();
        int pageSize = chatMessageForm.getPageSize();
        Long receiverId = chatMessageForm.getReceiverId();
        Long senderId = chatMessageForm.getSenderId();
        QueryWrapper<ChatMessage> chatMessageQueryWrapper = new QueryWrapper<>();
        chatMessageQueryWrapper.eq(receiverId != null,"receiver_id",receiverId);
        chatMessageQueryWrapper.eq(senderId != null,"sender_id",senderId);
        chatMessageQueryWrapper.or().eq(receiverId != null,"receiver_id",senderId);
        chatMessageQueryWrapper.eq(senderId != null,"sender_id",receiverId);
        chatMessageQueryWrapper.orderByDesc("timestamp");
        Page<ChatMessage> page = page(new Page<>(current, pageSize), chatMessageQueryWrapper);
        List<ChatMessage> collect = page.getRecords().stream().sorted(Comparator.comparing(ChatMessage::getTimestamp)).collect(Collectors.toList());
        page.setRecords(collect);
        return page;
    }

    @Override
    public List<UserVO> listChatWithUser(Long userId) {
        if (userId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Long> userIds = this.baseMapper.listChatWithUser(userId);
        if (userIds == null || userIds.size() == 0){
            return new ArrayList<>();
        }
        List<User> userList = userService.listByIds(userIds);
        return userList.stream()
                .map(user -> userService.getUserVO(user))
                .collect(Collectors.toList());
    }

    @Override
    public List<ChatMessage> listMessage(ChatMessageForm chatMessageForm) {
        Long receiverId = chatMessageForm.getReceiverId();
        Long senderId = chatMessageForm.getSenderId();
        QueryWrapper<ChatMessage> chatMessageQueryWrapper = new QueryWrapper<>();
        chatMessageQueryWrapper.eq(receiverId != null,"receiver_id",receiverId);
        chatMessageQueryWrapper.eq(senderId != null,"sender_id",senderId);
        chatMessageQueryWrapper.or().eq(receiverId != null,"receiver_id",senderId);
        chatMessageQueryWrapper.eq(senderId != null,"sender_id",receiverId);
        return list(chatMessageQueryWrapper);
    }
}




